package com.travel.search.controller;

import com.alibaba.fastjson.JSON;
import com.travel.common.vo.MemberRespVo;
import com.travel.search.service.TravelAttractionSearchService;
import com.travel.search.service.TravelTicketSearchService;
import com.travel.search.vo.SearchAttractionParam;
import com.travel.search.vo.SearchAttractionResult;
import com.travel.search.vo.SearchTicketParam;
import com.travel.search.vo.SearchTicketResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

import static com.travel.common.constant.AuthServerConstant.LOGIN_USER;

@Slf4j
@Controller
public class SearchController {

    @Autowired
    TravelAttractionSearchService travelAttractionSearchService;

    @Autowired
    TravelTicketSearchService travelTicketSearchService;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 景点的检索
     * 自动将页面提交过来的所有请求查询数据封装成指定的对象
     */
    @GetMapping("/attractionList.html")
    public String attractionListPage(SearchAttractionParam param, Model model, HttpServletRequest request)throws Exception{
        param.set_queryString(request.getQueryString());
        //1. 根据传递过来的页面查询数据，区es中检索商品
        SearchAttractionResult result = travelAttractionSearchService.search(param);
        model.addAttribute("result",result);
        log.info(result.toString());

        //2. 从reids中获取当前用户
        String user = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo loginUser = JSON.parseObject(user, MemberRespVo.class);

        model.addAttribute("loginUser",loginUser);

        return "attractionList";

    }

    /**
     * 票的检索
     * 自动将页面提交过来的所有请求查询数据封装成指定的对象
     */
    @GetMapping("/ticketList.html")
    public String ticketListPage(SearchTicketParam param, Model model, HttpServletRequest request)throws Exception{
        param.set_queryString(request.getQueryString());
        //1. 根据传递过来的页面查询数据，区es中检索商品
        SearchTicketResult result = travelTicketSearchService.search(param);
        model.addAttribute("result",result);
        log.info(result.toString());

        //2. 从reids中获取当前用户
        String user = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo loginUser = JSON.parseObject(user, MemberRespVo.class);

        model.addAttribute("loginUser",loginUser);

        return "ticketList";

    }
}
